var api = require('../../../config/api.js');
var check = require('../../../utils/check.js');

var app = getApp();
Page({
  data: {
    mobile: '',
    code: '',
    text: '获取验证码', //按钮文字
    currentTime: 61, //倒计时
    disabled: true, //按钮是否禁用
    disableds:false,
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function() {

  },
  onShow: function() {
    // 页面显示

  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },

  sendCode: function() {
    let that = this;
    that.setData({
      disabled: true, //只要点击了按钮就让按钮禁用 （避免正常情况下多次触发定时器事件）
      color: '#ccc',
    })
    var mobile = that.data.mobile;
    var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值
    var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空
    if (mobile == '') {
      warn = "号码不能为空";
    } else if (mobile.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(mobile)) {
      warn = "手机号格式不正确";
    } else {
      wx.request({
        url: api.AuthRegisterCaptcha,
        data: {
          mobile: that.data.mobile
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          if (res.data.errno == 0) {
            // wx.showToast({
            //   title: '发送成功',
            // });
          } else {
            // wx.showModal({
            //   title: '错误信息',
            //   content: res.data.errmsg,
            //   showCancel: false
            // });
          }
        }
      });
      var interval = setInterval(function() {
        currentTime--; //每执行一次让倒计时秒数减一
        that.setData({
          text: currentTime + 's', //按钮文字变成倒计时对应秒数
        })
        //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字
        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            text: '重新发送',
            currentTime: 61,
            disabled: false,
            color: '#15cc2d'
          })
        }
      }, 1000);
    }
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })

      that.setData({
        disabled: false,
        color: '#15cc2d'
      })
      return;
    };


  },
  startBind: function() {
    var that = this;
    let userInfo = wx.getStorageSync('userInfo');

    if (this.data.mobile.length == 0 || this.data.code.length == 0) {
      wx.showModal({
        title: '错误信息',
        content: '手机号和验证码不能为空',
        showCancel: false
      });
      return false;
    }

    if (!check.isValidPhone(this.data.mobile)) {
      wx.showModal({
        title: '错误信息',
        content: '手机号输入不正确',
        showCancel: false
      });
      return false;
    }
console.log(this.data);
console.log(userInfo)
    that.setData({
      disableds:true
    });
    wx.request({
      url: api.BindMobile,
      data: {
        mobile: that.data.mobile,
        code: that.data.code,
        openId: userInfo.openId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        // console.log(res);
        if (res.data.errno == 0) {
          that.setData({
            disableds: false
          });
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 2000,
            success: function() {
              wx.navigateBack(2);
              app.globalData.hasLogin = false;
              wx.removeStorageSync('token');
              wx.removeStorageSync('userInfo');
              wx.navigateTo({
                url: '/pages/auth/login/login'
              });
            }
          })
        } else {
          that.setData({
            disableds: false
          });
          wx.showModal({
            title: '绑定失败',
            content: res.data.errmsg,
            showCancel: false
          });
        }
      }
    });
  },

  bindMobileInput: function(e) {
    var mobile = e.detail.value;
    if (mobile == '') {
      this.setData({
        disabled: true
      })
    } else if (mobile.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(mobile)) {
      this.setData({
        disabled: true
      })
    } else {
      this.setData({
        disabled: false
      })
    }
    this.setData({
      mobile: e.detail.value
    });
  },
  bindCodeInput: function(e) {

    this.setData({
      code: e.detail.value
    });
  },
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-confirm-password':
        this.setData({
          confirmPassword: ''
        });
        break;
      case 'clear-mobile':
        this.setData({
          mobile: '',
          disabled: true
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  }
})